﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace Hired
{
    public partial class jslogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["error"] != null)
            {
                switch (int.Parse(Request.QueryString["error"]))
                {
                    case 0:
                        lblErrorMsg.Text = "Wrong Username or Password";
                        break;
                    case 1:
                        lblErrorMsg.Text = "Please fill in all the fields";
                        break;
                    default:
                        // do nothing
                        break;
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUser.Text != "" && txtPassword.Text != "")
            {
                JobseekerAccount accountToLogin = new JobseekerAccount();
                if (DBJobseekerAccount.retrieveJobseekerAccountbyUsername(txtUser.Text, false) != null)
                {
                    accountToLogin = DBJobseekerAccount.retrieveJobseekerAccountbyUsername(txtUser.Text, false);
                }
                if (Encryption.verify(txtPassword.Text, accountToLogin.Password))
                {
                    if ((bool)accountToLogin.Activated)
                    {
                        Session["username"] = accountToLogin.Username;
                        FormsAuthentication.RedirectFromLoginPage(accountToLogin.JAccountID, true);
                        Response.Redirect("home.aspx");
                    }
                    else
                    {
                        Session["usernameToActivate"] = accountToLogin.Username;
                        Response.Redirect("notactivated.aspx", false);
                    }
                }
                else
                {
                    lblErrorMsg.Text = "Wrong Username or Password";
                }
            }
            else
            {
                lblErrorMsg.Text = "Please fill in all the fields.";
            }
        }
    }
}